﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;
using Gollie.WebCache;

namespace Gollie
{
	public class VendorBranche : CacheFileDependency
	{
		public static string cacheKey = "data_vendorbranche";

		public VendorBranche()
			: base(HttpContext.Current.Server.MapPath("/App_Data/VendorBranche.xml"))
		{
			Branches = new Dictionary<int, BrancheInfo>();

			XmlDocument doc = new XmlDocument();

			doc.Load(this.DependencyPath);

			XmlNode baseNode = doc.SelectSingleNode("/gollie/branches");
			string baseImagePath = baseNode.Attributes["baseimagepath"].Value;

			XmlNodeList nodes = doc.SelectNodes("/gollie/branches/branche");

			if (nodes != null)
			{
				foreach (XmlNode node in nodes)
				{
					XmlAttribute attr = node.Attributes["id"];
					if (attr != null)
					{
						int id = XmlConvert.ToInt32(attr.Value);
						string name = node.Attributes["name"].Value;
						string path = baseImagePath + "/" + node.Attributes["image"].Value;

						Branches[id] = new BrancheInfo { Id = id, Name = name, Image = path };
					}
				}
			}
		}


		public Dictionary<int, BrancheInfo> Branches { get; private set; }

		public static VendorBranche GetInstance()
		{
			return DataCache.GetCacheableObject<VendorBranche>(cacheKey);
		}
	}

	public class BrancheInfo
	{
		public int Id;
		public string Name;
		public string Image;
	}
}